#include "GeneralHelperFunctions.h"

WindowSettings GetWindowSettings( std::string FileName )
{
	std::fstream File;
	File.open(FileName.c_str(), std::ios::in);

	WindowSettings RetWndSettings = WindowSettings();

	RetWndSettings.WindowStyle = sf::Style::None;
	RetWndSettings.WindowTitle = "TrainJumper";

	if(!File.is_open())
	{

		RetWndSettings.MainMode = sf::VideoMode(800, 600, 32);
		RetWndSettings.MainWindowSettings = sf::ContextSettings(8, 8, 8, 3, 0);
	}
	else
	{
		char FileBuffer[MAX_CHARS_PER_LINE+1];
		char TempBuffer[MAX_CHARS_PER_LINE];

		//Variables for testing 
		char *Height ="Height",
			 *Width ="Width",
			 *BitsPerPixel ="BitsPerPixel",
			 *DepthBits = "DepthBits",
			 *StencilBits = "StencilBits",
			 *AntiAliasingLevel = "AntiAliasingLevel",
			 *MajorVersion = "MajorVersion",
			 *MinorVersion = "MinorVersion";
		

		while(!File.eof())
		{
			ClearCharBuffer(FileBuffer, MAX_CHARS_PER_LINE+1);//Clears and initializes buffers to 0
			ClearCharBuffer(TempBuffer, MAX_CHARS_PER_LINE);

			File.getline(FileBuffer,MAX_CHARS_PER_LINE,';');
			if(CheckFrontCharString(FileBuffer, MAX_CHARS_PER_LINE+1, Height))
			{

				for(UINT i = strlen(Height); i < MAX_CHARS_PER_LINE; ++i)
				{
					if(FileBuffer[i] == ':')
						{//do nothing
						}
					else
						TempBuffer[i-(strlen(Height)+1)] = FileBuffer[i];

					if(FileBuffer[i] == NULL)
						i = MAX_CHARS_PER_LINE;
				}

				RetWndSettings.MainMode.height = CharToInt(TempBuffer);
			}

			if(CheckFrontCharString(FileBuffer, MAX_CHARS_PER_LINE+1, Width))
			{

				for(UINT i = strlen(Width); i < MAX_CHARS_PER_LINE; ++i)
				{
					if(FileBuffer[i] == ':')
						{//do nothing
						}
					else
						TempBuffer[i-(strlen(Width)+1)] = FileBuffer[i];

					if(FileBuffer[i] == NULL)
						i = MAX_CHARS_PER_LINE;
				}

				RetWndSettings.MainMode.width = CharToInt(TempBuffer);
			}

			if(CheckFrontCharString(FileBuffer, MAX_CHARS_PER_LINE+1, BitsPerPixel))
			{

				for(UINT i = strlen(BitsPerPixel); i < MAX_CHARS_PER_LINE; ++i)
				{
					if(FileBuffer[i] == ':')
						{//do nothing
						}
					else
						TempBuffer[i-(strlen(BitsPerPixel)+1)] = FileBuffer[i];

					if(FileBuffer[i] == NULL)
						i = MAX_CHARS_PER_LINE;
				}

				RetWndSettings.MainMode.bitsPerPixel = CharToInt(TempBuffer);
			}

			if(CheckFrontCharString(FileBuffer, MAX_CHARS_PER_LINE+1, DepthBits))
			{

				for(UINT i = strlen(DepthBits); i < MAX_CHARS_PER_LINE; ++i)
				{
					if(FileBuffer[i] == ':')
						{//do nothing
						}
					else
						TempBuffer[i-(strlen(DepthBits)+1)] = FileBuffer[i];

					if(FileBuffer[i] == NULL)
						i = MAX_CHARS_PER_LINE;
				}

				RetWndSettings.MainWindowSettings.depthBits = CharToInt(TempBuffer);
			}

			if(CheckFrontCharString(FileBuffer, MAX_CHARS_PER_LINE+1, StencilBits))
			{

				for(UINT i = strlen(StencilBits); i < MAX_CHARS_PER_LINE; ++i)
				{
					if(FileBuffer[i] == ':')
						{//do nothing
						}
					else
						TempBuffer[i-(strlen(StencilBits)+1)] = FileBuffer[i];

					if(FileBuffer[i] == NULL)
						i = MAX_CHARS_PER_LINE;
				}

				RetWndSettings.MainWindowSettings.stencilBits = CharToInt(TempBuffer);
			}

			if(CheckFrontCharString(FileBuffer, MAX_CHARS_PER_LINE+1, AntiAliasingLevel))
			{

				for(UINT i = strlen(AntiAliasingLevel); i < MAX_CHARS_PER_LINE; ++i)
				{
					if(FileBuffer[i] == ':')
						{//do nothing
						}
					else
						TempBuffer[i-(strlen(AntiAliasingLevel)+1)] = FileBuffer[i];

					if(FileBuffer[i] == NULL)
						i = MAX_CHARS_PER_LINE;
				}

				RetWndSettings.MainWindowSettings.antialiasingLevel = CharToInt(TempBuffer);
			}

			if(CheckFrontCharString(FileBuffer, MAX_CHARS_PER_LINE+1, MajorVersion))
			{

				for(UINT i = strlen(MajorVersion); i < MAX_CHARS_PER_LINE; ++i)
				{
					if(FileBuffer[i] == ':')
						{//do nothing
						}
					else
						TempBuffer[i-(strlen(MajorVersion)+1)] = FileBuffer[i];

					if(FileBuffer[i] == NULL)
						i = MAX_CHARS_PER_LINE;
				}

				RetWndSettings.MainWindowSettings.majorVersion = CharToInt(TempBuffer);
			}

			if(CheckFrontCharString(FileBuffer, MAX_CHARS_PER_LINE+1, MinorVersion))
			{

				for(UINT i = strlen(MinorVersion); i < MAX_CHARS_PER_LINE; ++i)
				{
					if(FileBuffer[i] == ':')
						{//do nothing
						}
					else
						TempBuffer[i-(strlen(MinorVersion)+1)] = FileBuffer[i];

					if(FileBuffer[i] == NULL)
						i = MAX_CHARS_PER_LINE;
				}

				RetWndSettings.MainWindowSettings.minorVersion = CharToInt(TempBuffer);
			}

			File.ignore(1); //Ignores the last character of the line
		}
	}
	return RetWndSettings;
}

bool CheckFrontCharString(char *MainString, UINT MainStringLength, std::string CheckFor)
{
	if(MainStringLength < CheckFor.length())
		return false;

	for(UINT i = 0; i < CheckFor.length(); ++i)
	{
		char a = MainString[i], b = CheckFor.c_str()[i];
		if(a != b)
			return false;
	}
	return true;
}

void ClearCharBuffer(char *ClearMe, UINT Length)
{
	for(UINT i = 0; i < Length; ++i)
	{
		ClearMe[i] = NULL;
	}
	ClearMe[Length-1] = '\0';
}

int CharToInt(char *ConvertMe)
{
	return atoi(ConvertMe);
}

float DegToRad(float Degrees)
{
	return float((Degrees*GAME_PI)/180);
}

float RadToDeg(float Radians)
{
	return float((Radians*180)/GAME_PI);
}

std::string GetStringFromChar( char *MainString )
{
	std::string returnString;
	UINT i = 0;
	while(MainString[i] != 0)
	{
		returnString += MainString[i];
		++i;
	}

	return returnString;

}